package be.destin.abdera;

import org.apache.abdera.protocol.server.CollectionAdapter;
import org.apache.abdera.protocol.server.ProviderHelper;
import org.apache.abdera.protocol.server.RequestContext;
import org.apache.abdera.protocol.server.RequestProcessor;
import org.apache.abdera.protocol.server.ResponseContext;
import org.apache.abdera.protocol.server.WorkspaceManager;
import org.apache.abdera.protocol.server.context.ResponseContextException;
import org.apache.abdera.protocol.server.provider.basic.BasicProvider;
import org.apache.log4j.Logger;

/*
 * TODO: Process collection(entry)/$links/application_role ...
 */
public class EntriesListRequestProcessor<T> implements RequestProcessor {

	/** log4j category */
	private static Logger log = Logger.getLogger(EntriesListRequestProcessor.class);

	/**
	 * An Atom Feed Document representing an Atompub Collection
	 */
	public ResponseContext process(RequestContext context,
			WorkspaceManager workspaceManager,
			CollectionAdapter collectionAdapter) {
		if (collectionAdapter == null) {
        	log.error(context.getTargetPath()+": no Collection adapter?");
			return ProviderHelper.notfound(context);
		} else {
			return this.processEntriesList(context, (AbstractCorrectedCollectionAdapter<T>)collectionAdapter);
		}
	}

	private ResponseContext processEntriesList(RequestContext context, AbstractCorrectedCollectionAdapter<T> adapter) {
		String method = context.getMethod();
		if (method.equalsIgnoreCase("GET")) {
			String entriesList = context.getTarget().getParameter(AbstractCorrectedWorkspaceProvider.ENTRIES_LIST);
			return adapter.getEntriesListFeed(context,entriesList);
		} else if (method.equalsIgnoreCase("POST")) {
			return ProviderHelper.notallowed(context);
		} else {
			return null;
		}
	}

}
